package ltd.latewhale.whalenest.controller;

import ltd.latewhale.whalenest.DTO.commentsCountDTO;
import ltd.latewhale.whalenest.model.commentsDO;
import ltd.latewhale.whalenest.model.commentsDO;
import ltd.latewhale.whalenest.service.commentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论信息
 */
@RestController
@CrossOrigin //跨域
@RequestMapping("/comments")
@Validated
public class commentsController {

    @Autowired
    private commentsService commentsService;

    //按id查询单个数据
    @GetMapping("/{id}")
    public commentsDO getcomments(
            @PathVariable String id){
        commentsDO commentss = commentsService.getById(id);

        System.out.println("查询id为 "+id+" 的数据");

        if(commentss == null){
            return null;
        }
        return  commentss;
    };

    //查询全部数据
    @GetMapping("")
    public List<commentsDO> getcommentss(){
        List<commentsDO> commentss = commentsService.getcomments();

        System.out.println("查询全部数据");

        if(commentss == null){
            return null;
        }
        return commentss;
    }
    
    //按id删除单个数据
    @DeleteMapping("/{id}")
    public Integer deletecommentss(
            @PathVariable Integer id
    ){
        System.out.println(id);

        Integer commentss = commentsService.deleteId(id);

        System.out.println(id);

        if(commentss == 0){
            return null;
        }
        return 200;
    };

    //新增数据
    @PostMapping("")
    public Integer postcomments(
            @RequestBody @Validated commentsDO dto
    ){
        System.out.println(dto);

        Integer comments = commentsService.postcomments(dto);

        System.out.println(dto);

        if(comments == null){
            return null;
        }
        return 200;
    }

    //评论数统计
    @GetMapping("/count")
    public List<commentsCountDTO> getCount(){
        List<commentsCountDTO> datas = commentsService.getCount();
        return datas;
    }
}
